package com.laboratory.miniolearning.controller;

import com.laboratory.miniolearning.domin.FileVO;
import com.laboratory.miniolearning.domin.UploadResult;
import com.laboratory.miniolearning.service.FileService;
import com.laboratory.miniolearning.service.UploadService;
import io.minio.StatObjectResponse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public FileVO upload(@RequestParam("file") MultipartFile file) {
        return uploadService.upload(file);
    }

    @PostMapping("/upload2")
    public UploadResult upload2(@RequestParam("file") MultipartFile file, @RequestParam("dir") String dir) {
        return fileService.upload(file, dir);
    }

    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> download(@RequestParam("objectName") String objectName) {
        try {
            // 1. 从 MinIO 获取文件流
            InputStream inputStream = fileService.download(objectName);
            // 2. 你可以通过 stat 查询文件信息，比如大小、ContentType，这里先写死
            String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
            String fileName = objectName.substring(objectName.lastIndexOf("/") + 1);

            return ResponseEntity.ok()
                    // 设置响应头：浏览器会提示下载
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\"")
                    .contentType(MediaType.parseMediaType(contentType))
                    // .contentLength(size) // 如果能获取大小，可以加上
                    .body(new InputStreamResource(inputStream));
        } catch (Exception e) {
            throw new RuntimeException("下载失败: " + e.getMessage(), e);
        }
    }

    @GetMapping("/download2")
    public void download2(String objectName) {
        fileService.downloadToFile(objectName, new File("F:/Software/Edge/" + objectName));
    }

    @DeleteMapping("/remove")
    public void remove(String objectName) {
        fileService.removeObjectName(objectName);
    }

    @GetMapping("/exists")
    public boolean exists(String objectName) {
        return fileService.exists(objectName);
    }

    @GetMapping("/stat")
    public StatObjectResponse stat(String objectName) {
        StatObjectResponse statObjectResponse = fileService.statObject(objectName);
        System.out.println(statObjectResponse.contentType());
        System.out.println(statObjectResponse.etag());
        return statObjectResponse;
    }

}
